package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.SignInService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class NewAccountServlet extends HttpServlet {
    private Account account;
    private String username;
    private String password;
    private String email;
    private String firstName;
    private String lastName;
    private String status;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String phone;
    private String favouriteCategoryId;
    private String languagePreference;
    private String listOption;
    private String bannerOption;
    private String bannerName;
    private Account account1;

    private static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        account1 = (Account) session.getAttribute("account");
        //account1 = null;

        //获得输入的验证码值
        String value1=request.getParameter("vCode");
        /*获取图片的值*/
        String value2=(String)session.getAttribute("checkcode");
        Boolean isSame = false;
        /*对比两个值（字母不区分大小写）*/
        if(value2.equalsIgnoreCase(value1)){
            isSame = true;
        }



        username = request.getParameter("username");
        password = request.getParameter("password");
        email = request.getParameter("email");
        firstName = request.getParameter("firstName");
        lastName = request.getParameter("lastName");
        status = request.getParameter("status");
        address1 = request.getParameter("address1");
        address2 = request.getParameter("address2");
        city = request.getParameter("city");
        state = request.getParameter("state");
        zip = request.getParameter("zip");
        country = request.getParameter("country");
        phone = request.getParameter("phone");
        favouriteCategoryId = request.getParameter("favouriteCategoryId");
        languagePreference = request.getParameter("languagePreference");
        listOption = request.getParameter("listOption");
        bannerOption = request.getParameter("bannerOption");
        bannerName = request.getParameter("bannerName");

        account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        account.setEmail(email);
        account.setFirstName(firstName);
        account.setLastName(lastName);
        account.setStatus(status);
        account.setAddress1(address1);
        account.setAddress2(address2);
        account.setCity(city);
        account.setState(state);
        account.setZip(zip);
        account.setCountry(country);
        account.setPhone(phone);
        account.setFavouriteCategoryId(favouriteCategoryId);
        account.setLanguagePreference(languagePreference);
        account.setListOption(Boolean.parseBoolean(listOption));
        account.setBannerOption(Boolean.parseBoolean(bannerOption));
        account.setBannerName(bannerName);

        session.setAttribute("account", account);



//        System.out.println("1");

        if(isSame){
            SignInService signInService = new SignInService();
            signInService.insertAccount(account);

            //System.out.println("2");
            if(account != null){
                //System.out.println("3");
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + "Register a new account";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            //System.out.println("6");

            request.getRequestDispatcher(MAIN).forward(request, response);
        }else {
            //System.out.println("4");
            session.setAttribute("messageAccount", "Invalid Verification Code.");

            if (account != null) {
                //System.out.println("5");
                HttpServletRequest httpRequest = request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();

                String logInfo = logService.logInfo(" ") + strBackUrl + " SignIn";
                logService.insertLogInfo(account.getUsername(), logInfo);

            }
            request.getRequestDispatcher(MAIN).forward(request, response);
        }

        //request.getRequestDispatcher(MAIN).forward(request,response);
    }
}
